var query = require("../../config/conn.js");
var mysql = require('../../lib/mysql');

class Article{
	constructor(){
		  //获取文章的总数
          this.totalCount = this.getTotalCount();
          //设置每页的数量
          this.count = 5;
	} 
	async getTotalCount(){
        var count = await mysql.table('blog').count('bloggerid');
        return count;
	}
	findAll(page){
		var _sql = "select * from blog limit ?,5";
		var params=[page];
		return new Promise(function(resolve,reject){
				query(_sql,params,function(err,result){
					if(err){
						return resolve([])
					}else{
						return resolve(result)
					}
		     
				})
		})
	
	}
	findOne(blogid){
		var _sql = "select * from blog where blogid = ?";
		var params = [blogid];
		return new Promise(function(resolve,reject){
				query(_sql,params,function(err,result){
					if(err){
						return resolve([])
					}else{
						return resolve(result)
					}
		     
				})
		})
	}
	findOnelist(bloggerid,page){
		var _sql = "select * from blog where bloggerid = ? limit ?,5";
		var params = [bloggerid,page];
		return new Promise(function(resolve,reject){
				query(_sql,params,function(err,result){
					if(err){
						return resolve([])
					}else{
						return resolve(result)
					}
		     
				})
		})
	}
	add(params){
	  var _sql = "insert into blog(title,bloginfo,readtimes,kinds,content,createtime,updatetime,headimg,bloggerid,author) values(?,?,?,?,?,?,?,?,?,?)";
		return new Promise(function(resolve,reject){
				query(_sql,params,function(err,result){
					if(err){
						return resolve([])
					}else{
						return resolve([result])
					}
		     
				})
		})
	}
	deleteOne(params){
		var _sql = "delete  from blog where blogid = ?";
		return new Promise(function(resolve,reject){
				query(_sql,params,function(err,result){
					if(err){
						return resolve([])
					}else{
						return resolve([result])
					}
		     
				})
		})
	}

}
module.exports = Article;